package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.EsignService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.esign.pojo.dto.EsignCorpRealnameDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFlowDetailDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFlowStatusDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignGetSwitchDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignPreCheckDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignSetSwitchDTO;
import com.xbongbong.pro.esign.pojo.dto.HasEsignDTO;
import com.xbongbong.pro.esign.pojo.dto.XbbFileDetailDTO;
import com.xbongbong.pro.esign.pojo.dto.XbbProcessStartDTO;
import com.xbongbong.pro.esign.pojo.dto.XbbSignNoticeUrlDTO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpConsoleVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpInfoVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpRealnameVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFileDetailVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFlowDetailVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFlowStatusVO;
import com.xbongbong.pro.esign.pojo.vo.EsignGetSwitchVO;
import com.xbongbong.pro.esign.pojo.vo.EsignNoticeUrlVO;
import com.xbongbong.pro.esign.pojo.vo.EsignPreCheckVO;
import com.xbongbong.pro.esign.pojo.vo.EsignProcessStartVO;
import com.xbongbong.pro.esign.pojo.vo.HasEsignVO;
import com.xbongbong.pro.esign.pojo.vo.RemindVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * e签宝
 * 创建时间 2020/6/17 11:27 AM
 * 修改时间 2020/6/17 11:27 AM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/esign")
public class EsignController {
    private static final Logger LOG = LoggerFactory.getLogger(EsignController.class);
    @Resource
    private EsignService esignService;

    /**
     * 提醒
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/remind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String remind(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<RemindVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RemindVO remindVO = esignService.remind(baseDTO);
                response = new XbbResponse<>(remindVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取文件详情
     * @param xbbFileDetailDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignFileDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignFileDetail(@RequestBody @Valid XbbFileDetailDTO xbbFileDetailDTO, BindingResult br) {
        XbbResponse<EsignFileDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFileDetailVO esignFileDetailVO = esignService.getEsignFileDetail(xbbFileDetailDTO);
                response = new XbbResponse<>(esignFileDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 前置校验
     * @param esignPreCheckDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/preCheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String preCheck(@RequestBody @Valid EsignPreCheckDTO esignPreCheckDTO, BindingResult br) {
        XbbResponse<EsignPreCheckVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignPreCheckVO esignPreCheckVO = esignService.preCheck(esignPreCheckDTO);
                response = new XbbResponse<>(esignPreCheckVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取发起签署任务地址
     * @param xbbProcessStartDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignProcessStart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignProcessStart(@RequestBody @Valid XbbProcessStartDTO xbbProcessStartDTO, BindingResult br) {
        XbbResponse<EsignProcessStartVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignProcessStartVO esignProcessStartVO = esignService.getEsignProcessStart(xbbProcessStartDTO);
                response = new XbbResponse<>(esignProcessStartVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取签署人签署地址
     * @param xbbSignNoticeUrlDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/signNoticeUrl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signNoticeUrl(@RequestBody @Valid XbbSignNoticeUrlDTO xbbSignNoticeUrlDTO, BindingResult br) {
        XbbResponse<EsignNoticeUrlVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignNoticeUrlVO esignNoticeUrlVO = esignService.signNoticeUrl(xbbSignNoticeUrlDTO);
                response = new XbbResponse<>(esignNoticeUrlVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查询企业信息
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignCorpInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignCorpInfo(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<EsignCorpInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignCorpInfoVO esignCorpInfoVO = esignService.getEsignCorpInfo(baseDTO.getCorpid());
                response = new XbbResponse<>(esignCorpInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取跳转到企业实名的地址
     * @param esignCorpRealnameDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignCorpRealname", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignCorpRealname(@RequestBody @Valid EsignCorpRealnameDTO esignCorpRealnameDTO, BindingResult br) {
        XbbResponse<EsignCorpRealnameVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignCorpRealnameVO esignCorpRealnameVO = esignService.getEsignCorpRealname(esignCorpRealnameDTO);
                response = new XbbResponse<>(esignCorpRealnameVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 判断实名情况（公司实名，当前人员实名）
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/checkRealname", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkRealname(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<EsignPreCheckVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignPreCheckVO esignPreCheckVO = esignService.checkRealname(baseDTO);
                response = new XbbResponse<>(esignPreCheckVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取企业控制台地址
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignCorpConsole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignCorpConsole(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<EsignCorpConsoleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignCorpConsoleVO esignCorpConsoleVO = esignService.getEsignCorpConsole(baseDTO.getCorpid());
                response = new XbbResponse<>(esignCorpConsoleVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 表单是否有开关
     * @param hasEsignDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/hasEsign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hasEsign(@RequestBody @Valid HasEsignDTO hasEsignDTO, BindingResult br) {
        XbbResponse<HasEsignVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HasEsignVO hasEsignVO = esignService.hasEsign(hasEsignDTO);
                response = new XbbResponse<>(hasEsignVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取开关电子合同
     * @param esignGetSwitchDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getSwitch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSwitch(@RequestBody @Valid EsignGetSwitchDTO esignGetSwitchDTO, BindingResult br) {
        XbbResponse<EsignGetSwitchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignGetSwitchVO esignGetSwitchVO = esignService.getSwitch(esignGetSwitchDTO);
                response = new XbbResponse<>(esignGetSwitchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置开关电子合同
     * @param esignFormSwitchDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/setSwitch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setSwitch(@RequestBody @Valid EsignSetSwitchDTO esignFormSwitchDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = esignService.setSwitch(esignFormSwitchDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取签署状态
     * @param esignFlowStatusDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignFlowStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignFlowStatus(@RequestBody @Valid EsignFlowStatusDTO esignFlowStatusDTO, BindingResult br) {
        XbbResponse<EsignFlowStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFlowStatusVO esignFlowStatusVO = esignService.getEsignFlowStatus(esignFlowStatusDTO);
                response = new XbbResponse<>(esignFlowStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取签署动态
     * @param esignFlowDetailDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:53 AM
     * 修改时间 2020/6/18 10:53 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignFlowInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignFlowInfo(@RequestBody @Valid EsignFlowDetailDTO esignFlowDetailDTO, BindingResult br) {
        XbbResponse<EsignFlowDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFlowDetailVO esignFlowDetailVO = esignService.getEsignFlowDetail(esignFlowDetailDTO);
                response = new XbbResponse<>(esignFlowDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
